import { addFeeToPayableApi, modifyFeeToPayableApi } from "@/api/financialManagement/payableManagement";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import { inject, Ref } from "vue";
export default function OperationHook(state: {
  type: Ref<number>;
  formState: StatementPayable.AddFeeToPayable;
  formDataRef: Ref<any>;
  emit: any;
  passData: Ref<Base.GeneraDataType>;
  loading: Ref<boolean>;
  waybillId: Ref<string>;
}) {
  const { formState, formDataRef, emit, passData, loading, type, waybillId } = state;
  const { resetFields } = FormHook({ formState });
  const statementInfo = inject<Ref<StatementPayable.ListRows>>("statementInfo");
  const submitHandle = () => {
    formDataRef.value.validate().then(() => {
      const params: any = {
        ...formState,
        waybillId: waybillId.value
      };
      let requestFn;
      if (type.value === 1) {
        Reflect.deleteProperty(params, "id");
        params.payableStatementId = statementInfo?.value.id;
        requestFn = addFeeToPayableApi;
      } else {
        params.id = passData.value.id;
        requestFn = modifyFeeToPayableApi;
      }
      loading.value = true;
      requestFn(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            emit("submit");
          }
        })
        .finally(() => (loading.value = false));
    });
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  return {
    submitHandle,
    resetHandle,
    resetFields
  };
}
